package com.tech.common

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties

object KafkaUtils {

  val props = new Properties
  var producer:KafkaProducer[String,String] = _

  def init(broker: String): Unit = {
    props.put("bootstrap.servers", broker) // broker 如果有多个,中间使用逗号分隔
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    producer = new KafkaProducer[String, String](props)
  }

  def write2Kafka(topic: String, msg: String): Unit = {
    val producerRecord = new ProducerRecord[String, String](topic, msg)
    producer.send(producerRecord).get()
  }

  def main(args: Array[String]): Unit = {
    init("dev-cdp-03:9092,dev-cdp-04:9092")
    write2Kafka("test1", "sdafd")
  }

}
